const EventEmitter = require('events');
// 定义 Dog 构造函数，继承 EventEmitter
function Dog(name, energy) {
  EventEmitter.call(this);
  this.name = name;
  this.energy = energy;
}
// 让 Dog 原型继承 EventEmitter 原型
Dog.prototype = Object.create(EventEmitter.prototype);
Dog.prototype.constructor = Dog;
// 定义 bark 方法，让狗狗每隔 1 秒叫一次，能量减 1，能量为 0 时停止
Dog.prototype.bark = function () {
  const timer = setInterval(() => {
    if (this.energy > 0) {
      console.log(`${this.name} barked! energy: ${this.energy}`);
      this.energy--;
    } else {
      clearInterval(timer);
    }
  }, 1000);
};
// 导出 Dog 构造函数
module.exports = Dog;